﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProjectWebNhac.BUS;
using System.Data;
using ProjectWebNhac.DTO;

namespace ProjectWebNhac.GUI.User
{
    public partial class TimKiem : System.Web.UI.Page
    {
        string Keywords = "";
        int Pages = 1;
        BaiHatBus baihatBus = new BaiHatBus();
        CaSiBus csBus = new CaSiBus();
        AlbumBus abBus = new AlbumBus();
        VideoBus vdBus = new VideoBus();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["keyword"]==null)
            {
                Response.Redirect("TrangChu.aspx");
            }
            Keywords = Request["keyword"].ToString();
            if (Session["ThanhVien"] != null)
            {
                NguoiDungDTO objUser = (NguoiDungDTO)Session["ThanhVien"];
                txtUserName.Text = objUser.TaiKhoan;
            }
            if (Request["page"] != null)
            {
                Pages = Convert.ToInt16(Request["page"]);
            }
            if (!Page.IsPostBack)
            {
                SearchResult();
                SearchResultCaSi();
                SearchResultAlbum();
                SearchResultVieo();
                loadPlaylist();
                
            }
        }
        //Bai hat
        private void SearchResult()
        {
            DataSet ds = baihatBus.TimKiemTheoTen(Pages, 20, 10, Keywords);
            DataTable dtbData = ds.Tables[0];
            DataTable dtbPT = null;
            if (dtbData.Rows.Count>0)
            {
                if (ds.Tables[1]!=null)
                {
                    dtbPT = ds.Tables[1];
                    dtbData.Columns.Add("RowIndex", typeof(System.Int32));
                    int i = 1;
                    foreach (DataRow dr in dtbData.Rows)
                    {
                        dr["RowIndex"] = i;
                        i++;
                    }
                    reSearch.DataSource = dtbData;
                    reSearch.DataBind();
                    liPage.Text = dtbPT.Rows[0][0].ToString();
                }
            }
        }
        // Ca Si
        private void SearchResultCaSi()
        {
            DataSet ds = csBus.TimKiemTheoTenCasi(Pages, 20, 10, Keywords);
            DataTable dtbData = ds.Tables[0];
            DataTable dtbPT = null;
            if (dtbData.Rows.Count > 0)
            {
                if (ds.Tables[1] != null)
                {
                    dtbPT = ds.Tables[1];
                    reCaSi.DataSource = dtbData;
                    reCaSi.DataBind();
                    liCaSi.Text = dtbPT.Rows[0][0].ToString();
                }
            }
        }
        //Album
        private void SearchResultAlbum()
        {
            DataSet ds = abBus.TimKiemTheoTenAlbum(Pages, 20, 10, Keywords);
            DataTable dtbData = ds.Tables[0];
            DataTable dtbPT = null;
            if (dtbData.Rows.Count > 0)
            {
                if (ds.Tables[1] != null)
                {
                    dtbPT = ds.Tables[1];
                    reAlbum.DataSource = dtbData;
                    reAlbum.DataBind();
                    liAlbum.Text = dtbPT.Rows[0][0].ToString();
                }
            }
        }
        //Video
        private void SearchResultVieo()
        {
            DataSet ds = vdBus.TimKiemTheoVideo(Pages, 20, 10, Keywords);
            DataTable dtbData = ds.Tables[0];
            DataTable dtbPT = null;
            if (dtbData.Rows.Count > 0)
            {
                if (ds.Tables[1] != null)
                {
                    dtbPT = ds.Tables[1];
                    reVideo.DataSource = dtbData;
                    reVideo.DataBind();
                    liVideo.Text = dtbPT.Rows[0][0].ToString();
                }
            }
        }
        protected void reSearch_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            RepeaterItem item = e.Item;
            CaSiBus csBus = new CaSiBus();
            int MaBaiHat = 0;
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView usrCurrent = e.Item.DataItem as DataRowView;
                MaBaiHat = Convert.ToInt16(usrCurrent["MaBaiHat"]);
            }
            if (item.ItemType == ListItemType.Item)
            {
                Repeater reCasi = (Repeater)e.Item.FindControl("reCasi");

                reCasi.DataSource = csBus.LayChiTietCasiTheoBaiHat(MaBaiHat);
                reCasi.DataBind();
            }
        }
        private void loadPlaylist()
        {
            if (Session["ThanhVien"] != null)
            {
                NguoiDungDTO objUser = (NguoiDungDTO)Session["ThanhVien"];
                PlaylistBus plBus = new PlaylistBus();
                dropPlaylist.DataSource = plBus.LayPlaylistTheoTaiKhoan(objUser.TaiKhoan);
                dropPlaylist.DataTextField = "TenPlaylist";
                dropPlaylist.DataValueField = "MaPlaylist";
                dropPlaylist.DataBind();
            }
        }

        protected void lnkBaiHat_Click(object sender, EventArgs e)
        {
            divBaiHat.Visible = true;
            divCaSi.Visible = false;
            divAlbum.Visible = false;
            divVideo.Visible = false;

            lnkBaiHat.CssClass = "active";
            lnkCaSi.CssClass = "";
            lnkVideo.CssClass = "";
            lnkAlbum.CssClass = "";
        }

        protected void lnkCaSi_Click(object sender, EventArgs e)
        {
            divBaiHat.Visible = false;
            divCaSi.Visible = true;
            divAlbum.Visible = false;
            divVideo.Visible = false;

            lnkBaiHat.CssClass = "";
            lnkCaSi.CssClass = "active";
            lnkVideo.CssClass = "";
            lnkAlbum.CssClass = "";
        }

        protected void lnkAlbum_Click(object sender, EventArgs e)
        {
            divBaiHat.Visible = false;
            divCaSi.Visible = false;
            divAlbum.Visible = true;
            divVideo.Visible = false;

            lnkBaiHat.CssClass = "";
            lnkCaSi.CssClass = "";
            lnkVideo.CssClass = "";
            lnkAlbum.CssClass = "active";
        }

        protected void lnkVideo_Click(object sender, EventArgs e)
        {
            divBaiHat.Visible = false;
            divCaSi.Visible = false;
            divAlbum.Visible = false;
            divVideo.Visible = true;

            lnkBaiHat.CssClass = "";
            lnkCaSi.CssClass = "";
            lnkVideo.CssClass = "active";
            lnkAlbum.CssClass = "";
        }
    }
}